package com.zqx.manager.controller;

import com.zqx.entity.Product;
import com.zqx.manager.common.RequestConstant;
import com.zqx.manager.service.ProductService;
import io.spring.gradle.dependencymanagement.org.codehaus.plexus.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(RequestConstant.PRODUCT_CONTROLLER_API)
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 创建产品
     * @param product
     * @return
     */
    @PostMapping(value = "")
    public Product addProduct(@RequestBody Product product){
        //log.info("[产品]创建,参数:{}",product);
        Product result = productService.addProduct(product);
        //log.info("[产品]创建,参数:{}",result);
        return result;
    }

    /**
     * 通过id查询单个产品
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Product findOne(@PathVariable String id){
        //log.info("查询单个产品,id={}",id);
        Product product = productService.findOne(id);
        //log.info("查询单个产品,product={}",product);
        return product;
    }

    /**
     * 分页复杂查询产品
     * @param ids
     * @param minRewardRate
     * @param maxRewardRate
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "")
    public Page<Product> query(String ids, BigDecimal minRewardRate,BigDecimal maxRewardRate,String status,
                               @RequestParam(defaultValue = "0") int pageNum,@RequestParam(defaultValue = "10") int pageSize){
        //log.info("【复杂查询】查询产品,ids={},minRewardRate={},maxRewardRate={},statusList={},pageNum={},pageSize={}",ids,minRewardRate,maxRewardRate,status,pageNum,pageSize);
        List<String> idList = null, statusList = null;
        if (!StringUtils.isEmpty(ids)){
            idList = Arrays.asList(ids);
        }
        if (!StringUtils.isEmpty(status)){
            statusList = Arrays.asList(status);
        }
        Pageable pageable = new PageRequest(pageNum, pageSize);
        Page<Product> page = productService.query(idList, minRewardRate, maxRewardRate, statusList, pageable);
        //log.info("[复杂查询]查询商品,结果page={}",page);
        return page;
    }
}
